﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GridViewLogoin
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(IsPostBack)
            {
                lblComment.Text = "";
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            var uid = Text_UsernName.Text.Trim();
            var pwd = Text_Password.Text.Trim();

            var sql = string.Format("select * from UserInfo where UserName = '{0}'", uid);
            var dt = DbHelper.GetData(sql);
            if(dt.Rows.Count == 1)
            {
                var item = dt.Rows[0];

                var IsActived = (bool)item.ItemArray[3];
                var IsDeleted = (bool)item.ItemArray[4];
                var PassWord = item.ItemArray[2].ToString();

                if(IsDeleted==true)
                {
                    lblComment.Text = "登入用户已被删除，无法登入，任何疑问请联系客服人员";
                }
                else
                {
                    if(IsActived == true)
                    {
                        if(pwd == PassWord)
                        {
                            // 用户密码，正确跳转网页
                            Response.Redirect("Default.aspx");
                        }
                        else
                        {
                            // 用户密码错误
                            lblComment.Text = "用户名或密码错误，请重试";
                        }
                    }
                    else
                    {
                        // 提示当前用户被禁用或冻结
                        lblComment.Text = "当前用户已被禁用或冻结，有任何疑问，请联系管理人员";
                    }
                }
            }
            else
            {
                lblComment.Text = "当前账号未注册，请确认";
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            var uid = Text_UsernName.Text.Trim();
            var pwd = Text_Password.Text.Trim();

            var sql = string.Format("select * from UserInfo where UserName='{0}'", uid);

            var res = DbHelper.GetData(sql);

            if (res.Rows.Count == 0)
            {
                var Register = string.Format("insert into UserInfo(UserName,PassWord) values('{0}','{1}')", uid, pwd);
                DbHelper.Exe(Register);
                lblComment.Text = "账号已经注册完成，可以使用啦";
            }
            else
            {
                lblComment.Text = uid + "已经被注册";
            }
        }
    }
}